package login;

import io.ClientSession;
import io.OperationResponse;

import java.util.HashMap;
import java.util.Map;

import loginmodel.Game;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import platform.AppConfig;
import protocol.ParameterType;
import protocol.ReturnCode;

public class UcLogin {
	protected static final Logger log = LoggerFactory.getLogger(UcLogin.class);

	public static void LoginUC(OperationResponse operationRequest,
			ClientSession session) {
		try {
			if (operationRequest.Parameters.size() < 2) {

				OperationResponse response = new OperationResponse(
						operationRequest.OperationCode, null, ReturnCode.No, "");
				session.SendOperationResponse(response);
			} else {
				String id = (String) operationRequest.Parameters
						.get(ParameterType.UserID);
				String pwd = (String) operationRequest.Parameters
						.get(ParameterType.UserPwd);
				String isGetIP = (String) operationRequest.Parameters
						.get(ParameterType.IsGetIP);
				String deviceID = (String) operationRequest.Parameters
						.get(ParameterType.DeviceID);
				String PhoneType=(String) operationRequest.Parameters
						.get(ParameterType.PhoneType);
				if (id.equals("zealm")  && pwd.equals("")) {
					OperationResponse response = new OperationResponse(
							operationRequest.OperationCode,
							AppConfig.dicAppInfo, ReturnCode.GetServer, "");
					session.SendOperationResponse(response);
					return;

				}
				SidInfoResponse.SidInfoResponseData ucDate = getUCID(id);
				if (ucDate == null) {

					return;
				} else {
					id = Integer.toString(ucDate.getUcid());
				}
				id = "uc" + id;
				operationRequest.Parameters.put(ParameterType.UserID, id);
				HashMap<Short, Object> myPrams = new HashMap<Short, Object>();
				myPrams.put(ParameterType.UserID, id);
				OperationResponse responseID = new OperationResponse(
						operationRequest.OperationCode, myPrams,
						ReturnCode.HasID, "");
				session.SendOperationResponse(responseID);
				Util.sdkReceived(operationRequest, session,id, deviceID, isGetIP,"uc",PhoneType,"");
			}

		} catch (Exception e) {
			log.debug(e.toString());

		}

	}

	//static String ServerUrl = "http://sdk.g.uc.cn/ss/";
	 static String ucServerUrl = "http://sdk.test4.g.uc.cn/ss";
	static int CpId = 28996;
	static int GameId = 538079;
	static String ChannelId = "2";
	static int ServerId = 2770;
	static String ApiKey = "676b9a7e267fc6f5ec9d2d1a2b4e2068";

	private static SidInfoResponse.SidInfoResponseData getUCID(String sid)
			throws Exception {
		if (sid.isEmpty()) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", System.currentTimeMillis());
		params.put("service", "ucid.user.sidInfo");
		Game game = new Game();
		game.setCpId(CpId);
		game.setGameId(GameId);
		game.setChannelId(ChannelId);

		game.setServerId(ServerId);

		params.put("game", game);

		Map data = new HashMap();
		data.put("sid", sid);
		params.put("data", data);

		params.put("encrypt", "md5");

		String signSource = CpId + "sid=" + sid + ApiKey;
		String sign = Util.getMD5Str(signSource);
		params.put("sign", sign);
		String body = Util.encodeJson(params);

		String result = Util.doPost(ucServerUrl, body);
		result = new String(result.getBytes("gbk"), "utf-8");
		SidInfoResponse rsp = (SidInfoResponse) Util.decodeJson(result,
				SidInfoResponse.class);

		if (rsp != null) {
			if (rsp.getState().getCode() == 1
					&& !rsp.getData().toString().isEmpty()) {
				return rsp.getData();

			}
		}
		return null;
	}

}
